package com.otherTest.other;

import com.otherTest.pojo.User;

import org.junit.Test;

import java.util.*;

/**
 * @author Conceit
 * @version 1.0, 2016/12/26
 */
public class TestMap {


    @Test
    public void map() {
        int rainfullData = 0;
        Map<Date, Float> dataMap1 = new HashMap<Date, Float>();
        dataMap1.put(new Date(), new Float(1));
        Map<Date, Float> dataMap2 = new HashMap<Date, Float>();
        dataMap2.put(new Date(), new Float(2));
        Map<Date, Float> dataMap3 = new HashMap<Date, Float>();
        dataMap3.put(new Date(), new Float(3));
        Map<Date, Float> dataMap11 = new HashMap<Date, Float>();
        dataMap11.put(new Date(), new Float(11));


        Map<String, Map<Date, Float>> rainfallDataMap = new HashMap<String, Map<Date, Float>>();
        rainfallDataMap.put("one", dataMap1);
        rainfallDataMap.put("eleven", dataMap11);
        rainfallDataMap.put("two", dataMap2);
        rainfallDataMap.put("three", dataMap3);

        //遍历map
        for (Map.Entry<String, Map<Date, Float>> entry : rainfallDataMap.entrySet()) {
            String key = entry.getKey();
            System.out.println("rainfallDataMap的key:" + key);
            for (Map.Entry<Date, Float> en : entry.getValue().entrySet()) {
                Date date = en.getKey();
                float fl = en.getValue();
                rainfullData += fl;
                // if(rainfullData>11){
                //  System.out.println(rainfullData+"大于11！！！");
                //}
                System.out.println(key + "对应的key value是" + date + fl);
            }


        }
        //list 转数组
        List<String> stringList = new ArrayList<String>();
        stringList.add("1");
        stringList.add("2");
        stringList.add("3");
        int size = stringList.size();
        String[] ids = (String[]) stringList.toArray(new String[size]);
        for (String s : ids) {
            System.out.println(s);
        }


    }

    //可变对象封装为map  可变对象作为map的k和v 是十分危险的
    @Test
    public void testObjectMap() {
        User user1 = new User();
        user1.setId(1);
        user1.setUsername("zhaohang");
        User user2 = new User();
        user2.setId(2);
        user2.setUsername("zhaojie");
        User user3 = new User();
        user3.setId(3);
        user3.setUsername("zhaodi");
        List<User> userList = new ArrayList();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        Map<User, User> result = new HashMap<User, User>();
        Map<User, Map<User, User>> userMap = new HashMap();


        for (User user : userList) {
            if (user.getId() == 1) {
                result.clear();
                result.put(user, user);
                userMap.put(user, result);
            }
            if (user.getId() == 2) {
                result.clear();
                result.put(user, user);
                userMap.put(user, result);
            }
            if (user.getId() == 3) {
                result.clear();
                result.put(user, user);
                userMap.put(user, result);
            }

        }
        ///////lamada/////////////////////
        new Thread(() -> System.out.println("开始遍历map")).start();
        new Thread(() -> System.out.println("----------------")).start();


        for (Map.Entry<User, Map<User, User>> entry : userMap.entrySet()) {
            User user = entry.getKey();
            System.out.println("userId:" + user.getId() + "    " + "username:" + user.getUsername());
            for (Map.Entry<User, User> entry1 : entry.getValue().entrySet()) {
                User userfor = entry1.getKey();
                User userfor2 = entry1.getValue();
                System.out.println("内部遍历的key userId 为：" + userfor.getId());
                System.out.println("内部遍历的value userId 为：" + userfor2.getId());
            }
        }

    }


}
